package com.spl.msplayer.settings;

import java.util.List;
import java.util.Set;

import com.spl.msplayer.PlayerService;
import com.spl.msplayer.R;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Build;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class SettingActivity extends PreferenceActivity {

	public static String VIDEO_PLAYBG_SETTING = "pref_video_playbg";
	public static String AUDIO_PLAYBG_SETTING = "pref_audio_playbg";
	
	/* (non-Javadoc)
	 * @see android.preference.PreferenceActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	/* (non-Javadoc)
	 * @see android.preference.PreferenceActivity#onBuildHeaders(java.util.List)
	 */
	@Override
	public void onBuildHeaders(List<Header> target) {
		// TODO Auto-generated method stub
		super.onBuildHeaders(target);
		loadHeadersFromResource(R.xml.preferences_header, target);
	}
	
	
	
	public static class MediaSettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener{

		/* (non-Javadoc)
		 * @see android.preference.PreferenceFragment#onCreate(android.os.Bundle)
		 */
		@Override
		public void onCreate(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);
			String pre_type = getArguments().getString("type");
			if(pre_type.equals("Video"))
				addPreferencesFromResource(R.xml.video_preferences);
			else 
				addPreferencesFromResource(R.xml.audio_preferences);
			
		}

		/* (non-Javadoc)
		 * @see android.app.Fragment#onPause()
		 */
		@Override
		public void onPause() {
			this.getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
			super.onPause();
			
		}

		/* (non-Javadoc)
		 * @see android.app.Fragment#onResume()
		 */
		@Override
		public void onResume() {
			this.getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
			super.onResume();
			
		}



		@Override
		public void onSharedPreferenceChanged(
				SharedPreferences sharedPreferences, String key) {
			// TODO Auto-generated method stub
			Preference pre = findPreference(key);
			boolean value = sharedPreferences.getBoolean(key, true);
			pre.setSummary(value?"On":"Off");
			PlayerService playService = PlayerService.getInstance();
			if(playService != null) {
				if(key.equals(SettingActivity.AUDIO_PLAYBG_SETTING))
					playService.setAudioBGPlay(value);
				else if(key.equals(SettingActivity.VIDEO_PLAYBG_SETTING))
					playService.setVideoBGPlay(value);
			}
		}
		
	}
}
